package com.dwxt.mall.admin.controller;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.mall.admin.service.ProductServiceAdmin;
import com.dwxt.mall.entity.Product;
import com.dwxt.mall.entity.vo.ProductVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/mall/product")
@RestController
public class ProductControllerAdmin {

    @Autowired
    private ProductServiceAdmin productServiceAdmin;

    @RequestMapping("/createOrUpdateProduct")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "price", minVal = 0),
            @ValidateFiled(index = 0, filedName = "integral", minVal = 0),
            @ValidateFiled(index = 0, filedName = "stock", minVal = 0),
            @ValidateFiled(index = 0, filedName = "coverImgUrl", notNull = true),
            @ValidateFiled(index = 0, filedName = "covers", notNull = true),
            @ValidateFiled(index = 0, filedName = "desp", notNull = true),
            @ValidateFiled(index = 0, filedName = "detail", notNull = true),
    })
    public BaseResult createOrUpdateProduct(ProductVO productVO) {
        if (productVO.getCovers().size() > 5) {
            return BaseResult.build(StatusCode.IMG_ERR);
        }
        productServiceAdmin.createOrUpdateProduct(productVO);
        return BaseResult.ok();
    }

    @RequestMapping("/selectProductPage")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            @ValidateFiled(index = 1, notNull = true),
    })
    public BaseResult selectProductPage(Integer pageNum, Integer pageSize, Product product) {
        return productServiceAdmin.selectProductList(pageNum, pageSize, product);
    }


    @RequestMapping("/delete")
    public BaseResult delete(Integer id) {
        productServiceAdmin.delete(id);
        return BaseResult.ok();
    }
}
